﻿namespace YahooSamples
{
    using System;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.IO;
    using System.Text;
    using System.Web;
    using DotNetOpenAuth.ApplicationBlock;
    using DotNetOpenAuth.OAuth;
    using OpenIdRelyingPartyWebForms.Code;

    public class Global : HttpApplication
    {
        
        internal static StringBuilder LogMessages = new StringBuilder();

        internal static WebConsumer YahooWebConsumer
        {
            get
            {
                var yahooWebConsumer = (WebConsumer)HttpContext.Current.Application["YahooWebConsumer"];
                if (yahooWebConsumer == null)
                {
                    yahooWebConsumer = new WebConsumer(YahooConsumer.ServiceDescription, YahooTokenManager);
                    HttpContext.Current.Application["YahooWebConsumer"] = yahooWebConsumer;
                }

                return yahooWebConsumer;
            }
        }

        internal static InMemoryTokenManager YahooTokenManager
        {
            get
            {
                var tokenManager = (InMemoryTokenManager)HttpContext.Current.Application["YahooTokenManager"];
                if (tokenManager == null)
                {
                    string consumerKey = ConfigurationManager.AppSettings["yahooConsumerKey"];
                    string consumerSecret = ConfigurationManager.AppSettings["yahooConsumerSecret"];
                    string appID = ConfigurationManager.AppSettings["yahooAppID"];
                    if (!string.IsNullOrEmpty(consumerKey))
                    {
                        tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
                        HttpContext.Current.Application["YahooTokenManager"] = tokenManager;
                    }
                }

                return tokenManager;
            }
        }

        internal static InMemoryTokenManager OwnSampleOPHybridTokenManager
        {
            get
            {
                var tokenManager = (InMemoryTokenManager)HttpContext.Current.Application["OwnSampleOPHybridTokenManager"];
                if (tokenManager == null)
                {
                    string consumerKey = new Uri(HttpContext.Current.Request.Url, HttpContext.Current.Request.ApplicationPath).AbsoluteUri;
                    string consumerSecret = "some crazy secret";
                    tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
                    HttpContext.Current.Application["OwnSampleOPHybridTokenManager"] = tokenManager;
                }

                return tokenManager;
            }
        }

        public static string ToString(NameValueCollection collection)
        {
            using (StringWriter sw = new StringWriter())
            {
                foreach (string key in collection.Keys)
                {
                    sw.WriteLine("{0} = '{1}'", key, collection[key]);
                }
                return sw.ToString();
            }
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            
        }

        protected void Application_End(object sender, EventArgs e)
        {
            
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            
        }

        private static string stripQueryString(Uri uri)
        {
            UriBuilder builder = new UriBuilder(uri);
            builder.Query = null;
            return builder.ToString();
        }
    }
}